<template>
  <div class="Medio-list">
    <div
      class="MedioBox"
      v-for="(subject, index) in SubjectList"
      :key="index"
      @mouseover="showDelBtn(index)"
      @mouseleave="showDelBtn(null)"
    >
      <div class="Medio">
        <div @click="toMainFace(subject.id)">
          <div class="main_box">
            <h2>{{ subject.title }}</h2>
            <div class="media">
              <div class="media-left media-middle" v-if="subject.user">
                <img
                  class="media-object"
                  :src="imgUrl[subject.user.imgCode]"
                  alt="..."
                />
              </div>
              <div class="media-body">
                <h4 class="media-heading">
                  {{ subject.user ? subject.user.name : "未知用户" }}
                </h4>
                <div class="media-heading teMin">{{ subject.CreateTime }}</div>
                <div class="media-heading">{{ subject.text }}</div>
              </div>
            </div>
          </div>
        </div>
        <div
          v-show="delBtn === index"
          class="displayNone"
          @click="showDelBtns1(index, subject.userId)"
        >
          <img class="point" src="/images/7.png" alt="" />
          <div></div>
        </div>
        <div
          class="delBtn"
          v-show="delBtn1 === index"
          @click="deleteSub(subject.id)"
        >
          删除
        </div>
      </div>
    </div>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
import instance from "@/utils/request";
import * as jose from "jose";
export default {
  data() {
    return {
      subjectList: [],
      imgUrl: [
        "/images/1.jpg",
        "/images/1.jpg",
        "/images/2.jpg",
        "/images/3.jpg",
        "/images/4.jpg",
        "/images/5.jpg",
        "/images/6.jpg",
      ],
      delBtn: null,
      delBtn1: null,
      TkId: null,
      currentPage: 1,
      pageSize: 5,
      size: "default",
      background: false,
      disabled: false,
      subjectList: [],
      total: 0,
    };
  },
  mounted() {
    this.getSubject();
    if (localStorage.getItem("token")) {
      const tokenDecode = jose.decodeJwt(localStorage.getItem("token"));
      this.TkId = tokenDecode.id;
    }
  },
  methods: {
    getSubject() {
      instance
        .get("subject", {
          params: {
            subject: this.SearchList,
            pageSize: this.pageSize,
            page: this.currentPage,
          },
        })
        .then((result) => {
          this.subjectList = result.data.data.subjectList;
          this.total = result.data.data.total;
        });
    },

    toMainFace(num) {
      this.$router.push({ name: "subject", params: { id: num } });
    },
    showDelBtn(index) {
      this.delBtn = index;
    },
    showDelBtns1(index, userId) {
      if (userId == this.TkId) {
        this.delBtn1 = index;
        setTimeout(() => {
          this.delBtn1 = null;
        }, 1000);
      }
    },
    deleteSub(sub) {
      instance.delete(`subject/${sub}`).then(window.location.reload());
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getSubject();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSubject();
    },
  },
  computed: {
    SubjectList() {
      return this.subjectList.map((subject) => {
        return {
          ...subject,
          CreateTime: dayjs(subject.createTime).format("YYYY-MM-DD"),
        };
      });
    },
    SearchList() {
      const data = this.$store.getters.getSearchList;
      return data;
    },
  },
  watch: {
    SearchList: {
      immediate: true, // 立即执行一次
      handler() {
        this.getSubject();
      },
    },
  },
};
</script>

<style>
.point {
  width: 20px;
  height: 20px;
  margin-top: 1%;
  margin-left: 108%;
  /* border: solid 1px #000; */
}

.Medio-list {
  width: 60vw;
  height: 100%;
  padding-top: 10px;
  /* border: solid 1px #000; */
}

.MedioBox {
  width: 100%;
  height: auto;
  min-height: 23vh;
  background-color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  /* border: solid 1px #000; */
}

.Medio {
  width: auto;
  max-width: 80%;
  height: auto;
  padding-left: -14px;
  margin: 0 auto;
  /* border: solid 1px #000; */
}

.media-object {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}

.main_box {
  width: 300px;
  height: auto;
  min-height: 10vh;
  background-color: #fff;
  border-radius: 10px;
}

.media {
  display: flex;
  /* border: solid 1px #000; */
}

.media-body {
  /* border: solid 1px #000; */
  width: 300px;
  padding-left: 10px;
}

.media-heading {
  /* border: solid 1px #000; */
  margin: 0px;
  padding: 0px;
}

.teMin {
  color: gray;
  font-size: 12px;
  padding: 5px;
}

.delBtn {
  width: 40px;
  height: 24px;
  margin-left: 106.4%;
  margin-top: -4%;
  border: solid 1px #e6e8e9;
  border-radius: 10px;
  background-color: #fff;
  text-align: center;
  font-size: 12px;
  padding-top: 10px;
}

.displayNone {
  /* border: solid 1px #000; */
  width: 100%;
  height: 0;
}

.demo-pagination-block {
  margin-top: 10px;
  margin-left: 20%;
}

.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>